
package com.hlkj.pay.app.ad.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.hlkj.pay.app.ad.constant.AdConstant;
import com.hlkj.pay.dto.ad.AdOtherConfigDto;
import com.hlkj.pay.infrastructure.model.ad.AdInfoConfigDO;
import com.hlkj.pay.infrastructure.model.ad.AdInfoDO;
import com.hlkj.pay.infrastructure.model.ad.AdPayOrderDO;
import com.hlkj.pay.infrastructure.model.ad.AdRefundOrderDO;
import com.hlkj.pay.infrastructure.model.user.UserTeleChargeOrderDO;
import com.hlkj.pay.util.SignUtils;

import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/24 13:36
 */
@RequiredArgsConstructor
@Slf4j
@Component
public class AdPayOrderHandlerParams {

    @Value("${hlkj.domain}")
    private String domain;

    public Map<String, Object> submitPayParams(AdPayOrderDO payOrderDO, AdInfoDO adInfoDO, AdInfoConfigDO adInfoConfigDO) throws Exception {
        // 示例参数
        Map<String, Object> params = new HashMap<>();
        params.put("ins_id", adInfoConfigDO.getSn());
        params.put("app_id", adInfoConfigDO.getAppId());
        params.put("sign_type", "RSA2");
        params.put("timestamp", System.currentTimeMillis() / 1000);

        Map<String, Object> biz_content = new HashMap<>();
        // biz_content.put("way_scene", 2);
        // biz_content.put("way_type", way_type(payType));
        // biz_content.put("way_code", way_code(payType, paySubType));
        // 1 微信 2 支付宝 3 京东白条 微信 ：offWechatLiteH5 支付宝：offAlipayQr 京东扫码：jdbt
        String way_code = "";
        if (payOrderDO.getPayType() == null) {
            payOrderDO.setPayType(1);
        }
        switch (payOrderDO.getPayType()) {
            case 1:
                way_code = "offWechatLiteH5";
                break;
            case 2:
                way_code = "onAlipayQr";
                break;
            case 3:
                way_code = "jdbt";
                break;
        }
        biz_content.put("way_code", way_code);
        biz_content.put("mch_order_no", payOrderDO.getMchOrderNo());
        biz_content.put("amount", payOrderDO.getAmount().doubleValue());
        // 获取request
        biz_content.put("client_ip", payOrderDO.getClientIp());
        biz_content.put("subject", payOrderDO.getSubject());
        biz_content.put("body", payOrderDO.getDescription());
        biz_content.put("notify_url", domain + AdConstant.AD_TRANS_ASYNC_CALLBACK_URL + payOrderDO.getAdId());
        biz_content.put("return_url", adInfoDO.getReturnUrl());

        params.put("biz_content", JSONUtil.toJsonStr(biz_content));
        String sign = SignUtils.signRsa(params, adInfoConfigDO.getPrivateKey(), null);
        log.info("pay params:{}, sign:{}", JSONUtil.toJsonStr(params), sign);
        params.put("sign", sign);
        return params;
    }

    public Map<String, Object> submitRefundParams(AdRefundOrderDO adRefundOrderDO, AdInfoConfigDO adInfoConfigDO) throws Exception {
        // 示例参数
        Map<String, Object> params = new HashMap<>();
        params.put("ins_id", adInfoConfigDO.getSn());
        params.put("app_id", adInfoConfigDO.getAppId());
        params.put("sign_type", "RSA2");
        params.put("timestamp", System.currentTimeMillis() / 1000);

        Map<String, Object> biz_content = new HashMap<>();
        biz_content.put("mch_order_no", adRefundOrderDO.getMchOrderNo());
        biz_content.put("mch_refund_no", adRefundOrderDO.getRefundOrderNo());
        biz_content.put("refund_amount", adRefundOrderDO.getRefundAmount());
        biz_content.put("refund_reason", adRefundOrderDO.getRefundReason());
        biz_content.put("notify_url", domain + AdConstant.AD_REFUND_ASYNC_CALLBACK_URL + adRefundOrderDO.getAdId());
        params.put("biz_content", JSONUtil.toJsonStr(biz_content));
        String sign = SignUtils.signRsa(params, adInfoConfigDO.getPrivateKey(), null);
        log.info("refund params:{}, sign:{}", JSONUtil.toJsonStr(params), sign);
        params.put("sign", sign);
        return params;
    }

    public Map<String, Object> queryPayParams(String mchOrderNo, AdInfoConfigDO adInfoConfigDO) throws Exception {
        // 示例参数
        Map<String, Object> params = new HashMap<>();
        params.put("ins_id", adInfoConfigDO.getSn());
        params.put("app_id", adInfoConfigDO.getAppId());
        params.put("sign_type", "RSA2");
        params.put("timestamp", System.currentTimeMillis() / 1000);
        Map<String, Object> biz_content = new HashMap<>();
        biz_content.put("mch_order_no", mchOrderNo);
        params.put("biz_content", JSONUtil.toJsonStr(biz_content));
        String sign = SignUtils.signRsa(params, adInfoConfigDO.getPrivateKey(), null);
        log.info("query pay params:{}, sign:{}", JSONUtil.toJsonStr(params), sign);
        params.put("sign", sign);
        return params;
    }

    public Map<String, Object> queryRefundParams(String refundOrderNo, AdInfoConfigDO adInfoConfigDO) throws Exception {
        // 示例参数
        Map<String, Object> params = new HashMap<>();
        params.put("ins_id", adInfoConfigDO.getSn());
        params.put("app_id", adInfoConfigDO.getAppId());
        params.put("sign_type", "RSA2");
        params.put("timestamp", System.currentTimeMillis() / 1000);
        Map<String, Object> biz_content = new HashMap<>();
        biz_content.put("mch_refund_no", refundOrderNo);
        params.put("biz_content", JSONUtil.toJsonStr(biz_content));
        String sign = SignUtils.signRsa(params, adInfoConfigDO.getPrivateKey(), null);
        log.info("query refund params:{}, sign:{}", JSONUtil.toJsonStr(params), sign);
        params.put("sign", sign);
        return params;
    }

    public Map<String, Object> rechargePayParams(Long adId, AdOtherConfigDto otherConfigDto, AdOtherConfigDto.CouponConfig couponConfig,
            UserTeleChargeOrderDO userTeleChargeOrderDO) throws Exception {
        // 示例参数
        Map<String, Object> params = new HashMap<>();
        params.put("out_trade_num", userTeleChargeOrderDO.getChargeNo());
        params.put("product_id", otherConfigDto.getProductId());
        params.put("mobile", userTeleChargeOrderDO.getPhone());
        params.put("userid", otherConfigDto.getAgentId());
        params.put("notify_url", domain + AdConstant.AD_RECHARGE_ASYNC_CALLBACK_URL + adId);
        String sign = SignUtils.signMd5(params, otherConfigDto.getAppKey());
        log.info("query refund params:{}, sign:{}", JSONUtil.toJsonStr(params), sign);
        params.put("sign", sign);
        return params;
    }
}
